package springboot.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class LogAspect {

    @Before("execution(* springboot.service.impl.*.*(..))")
    @Order(1) //不同的通知好像不起作用 around总是最先
    public void logBefore(JoinPoint jp) {
        Method method = ((MethodSignature) jp.getSignature()).getMethod();
        log.info("===before method: {}", method.getName());
    }

    @Around("execution(* springboot.service.impl.*.*(..))")
    @Order(3)
    public Object logAround(ProceedingJoinPoint pj) throws Throwable {
        Method method = ((MethodSignature) pj.getSignature()).getMethod();
        log.info("===around method: {}", method.getName());
        Object o = pj.proceed();
        log.info("===around2 method: {}", method.getName());
        return o;
    }
}
